// pages/home/home.js
import navList from '../../utils/nav'
import { getBannerListData, getProListData } from '../../api/home'
Page({
  data: {
    bannerList: [],
    navList,
    proList: [],
    count: 2,
    scrollTop: 0
  },
  onLoad() {
    getBannerListData().then(res => {
      console.log(res);
      this.setData({
        bannerList: res.data.data
      })
    })

    getProListData().then(res => {
      console.log(res.data.data);
      this.setData({
        proList: res.data.data
      })
    })
  },

  onPullDownRefresh() {
    getProListData().then(res => {
      console.log(res.data.data)
      this.setData({
        proList: res.data.data,
        count: 2
      })
      //停止下拉
      wx.stopPullDownRefresh()
    })
  },

  onReachBottom () {
    getProListData({ count: this.data.count }).then(res => {
      if (res.data.data.length === 0) {
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none'
        })
      } else {
        // 有下一页数据,实现数据的拼接、页码加1
        this.setData({
          proList: [...this.data.proList, ...res.data.data],
          count: this.data.count + 1
        })
      }
    })
  },

  onPageScroll ({ scrollTop }) {
    // console.log(scrollTop)
    this.setData({
      scrollTop
    })
  },

  backTop () {
    wx.pageScrollTo({
      duration: 1000,
      scrollTop: 0
    })
  }
})